package ck.android.englishschool.popup;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import ck.android.englishschool.AppStatus;
import ck.android.englishschool.R;
import ck.android.englishschool.thread.BaseThread.ThreadCallBack;
import ck.android.englishschool.thread.WordDatailThread;
import ck.android.englishschool.vo.WordDetailVO;

public class WordDetailActivity extends BaseActivity implements OnClickListener, OnItemSelectedListener, ThreadCallBack{
	private WordDatailThread thread;
	private String wordSeq, word;
	private WordDetailVO wordDetailVo;
	private TextView text_word, text_total_cnt, text_cur_cnt, text_kWord, text_percent;
	private Button btn_next, btn_kWord, btn_pre, btn_word, btn_ok;
	private ImageButton btn_red, btn_blue, btn_yellow, btn_black;
	private ImageButton btn_first_pre, btn_last_next;
	private ArrayList<WordDetailVO> returnVO;
	
	private int cnt;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_THREAD_COMPLETED:
				returnVO = myapp.getArrWordDetail();
				layoutSetting();
				break;
				
			case MSG_THREAD_ERROR:
				
				break;
				
			case MSG_NETWORK_ERROR:
				
				break;
				
			case MSG_UPDATE_COMPLETED:
				
				break;
				
			default:
				
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_word_detail);
		
		myapp = (AppStatus) getApplicationContext();
		context = getApplicationContext();
		
		intent = getIntent();
		wordSeq = intent.getStringExtra("seq");
		word = intent.getStringExtra("title");
		wordDetailVo = new WordDetailVO();
		wordDetailVo.setTitle(word);
		
		Log.i("seq", "------->"+wordSeq);
		
		init();
		
	}
	
	@Override
	public void threadCompleted(boolean isNetWordConnected, Object result,
			String message) {
		if(null != myapp.dialog || myapp.dialog.isShowing()){
			myapp.dialog.dismiss();
		}
		
		if((Boolean) result){
			msg = handler.obtainMessage();
			msg.what = MSG_THREAD_COMPLETED;
			handler.sendMessage(msg);
		}
	}
	
	@Override
	public void threadError(String message) {
		
	}
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_next:
			if(cnt<returnVO.size()-1){
				cnt += 1;
				viewWord(cnt);
			}
			break;
			
		case R.id.btn_kWord:
			if(text_kWord.getVisibility()==8){
				text_kWord.setVisibility(View.VISIBLE);
			}else{
				text_kWord.setVisibility(View.GONE);
			}
			break;
			
		case R.id.btn_pre:
			if(cnt<=returnVO.size() && cnt > 0){
				cnt -= 1;
				viewWord(cnt);
			}
			break;
			
		case R.id.btn_first_pre:
			cnt = 0;
			viewWord(cnt);
			break;
			
		case R.id.btn_last_next:
			cnt = returnVO.size() - 1 ;
			viewWord(cnt);
			break;
			
		case R.id.btn_word:
			if(text_word.getVisibility()==8){
				text_word.setVisibility(View.VISIBLE);
			}else{
				text_word.setVisibility(View.GONE);
			}
			break;
			
		case R.id.btn_ok:
			if(returnVO.size() > 1 && (returnVO.size()-1) != cnt){
				returnVO.remove(cnt);
				viewWord(cnt); 
			}else{
				System.out.println(cnt+", "+myapp.getArrWordDetail().size());
				//단어를 다 외운 상황 우선 다위운것은 아님
				if(myapp.getArrWordDetail().size()==1 && cnt==0){
					//마지막 단어 까지 다 외운 상황
					new AlertDialog.Builder(this).setTitle("Test").setMessage("시험볼래?")
					.setPositiveButton("예", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							endTest(word);
						}
					})
					.setNegativeButton("아니요", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							init();
						}
					}).setCancelable(false).show();
					
					return;
				}
				returnVO.remove(cnt);
				cnt=0;
				viewWord(cnt);
				
			}
			break;
			
		case R.id.btn_red:
			myapp.getArrWordDetail().get(cnt).setWordColor("red");
			viewWord(cnt);
			break;
			
		case R.id.btn_blue:
			myapp.getArrWordDetail().get(cnt).setWordColor("blue");
			viewWord(cnt);
			break;
	
		case R.id.btn_yellow:
			myapp.getArrWordDetail().get(cnt).setWordColor("yellow");
			viewWord(cnt);
			break;
			
		case R.id.btn_black:
			myapp.getArrWordDetail().get(cnt).setWordColor("black");
			viewWord(cnt);
			break;
			
		default:
			break;
		}
	}
	
	public void endTest(String title){
		intent = new Intent(this, WordTestActivity.class);
		intent.putExtra("title", title);
		startActivity(intent);
		finish();
	}
	
	public void viewWord(int cnt){
		text_word		.setText(returnVO.get(cnt).getWord().toString());
		text_cur_cnt	.setText((cnt+1)+"");
		text_total_cnt	.setText(returnVO.size()+"");
		text_kWord		.setText(returnVO.get(cnt).getkWord().toString());
		colorIn(myapp.getArrWordDetail().get(cnt).getWordColor());
	}
	
	public void colorIn(String color){
		String strColor = "#000000";
		if(null == color){
			color = "black";
		}
		switch (color) {
		case "red" :
			strColor = "#FF0000";
			text_word.setTextColor(Color.parseColor(strColor)); 
			break;

		case "blue" :
			strColor = "#0100FF";
			text_word.setTextColor(Color.parseColor(strColor)); 
			break;
			
		case "yellow" :
			strColor = "#FFE400";
			text_word.setTextColor(Color.parseColor(strColor)); 
			break;
			
		default:
			text_word.setTextColor(Color.parseColor(strColor)); 
			break;
		}
	}

	@Override
	public void layoutSetting() {
		viewWord(cnt);
	}
 
	@Override
	public void init() {
		context = this;
		cnt = 0;
		myapp.showDialogNo("데이터 로딩중", context);
		text_word 		= (TextView)findViewById(R.id.text_word);
		text_percent 	= (TextView)findViewById(R.id.text_percent);
		text_cur_cnt 	= (TextView)findViewById(R.id.text_cur_cnt);
		text_total_cnt 	= (TextView)findViewById(R.id.text_total_cnt);
		text_kWord 		= (TextView)findViewById(R.id.text_kWord);
		
		btn_red 		= (ImageButton)findViewById(R.id.btn_red);
		btn_blue 		= (ImageButton)findViewById(R.id.btn_blue);
		btn_yellow 		= (ImageButton)findViewById(R.id.btn_yellow);
		btn_black		= (ImageButton)findViewById(R.id.btn_black);
		btn_ok 			= (Button)findViewById(R.id.btn_ok);
		btn_word 		= (Button)findViewById(R.id.btn_word);
		btn_first_pre 	= (ImageButton)findViewById(R.id.btn_first_pre);
		btn_last_next 	= (ImageButton)findViewById(R.id.btn_last_next);
		btn_pre 		= (Button)findViewById(R.id.btn_pre);
		btn_next 		= (Button)findViewById(R.id.btn_next);
		btn_kWord 		= (Button)findViewById(R.id.btn_kWord);
		
		btn_red.		setOnClickListener(this);
		btn_blue.		setOnClickListener(this);
		btn_yellow.		setOnClickListener(this);
		btn_black.		setOnClickListener(this);
		btn_ok.			setOnClickListener(this);
		btn_word.		setOnClickListener(this);
		btn_first_pre.	setOnClickListener(this);
		btn_last_next.	setOnClickListener(this);
		btn_pre.		setOnClickListener(this);
		btn_next.		setOnClickListener(this);
		btn_kWord.		setOnClickListener(this);
		dataLoad();
	}

	@Override
	public void dataLoad() {
		thread = new WordDatailThread(context, this, wordDetailVo);
		thread.start();
	}
}
